package com.ryder.petmatediarybackend.module.shop.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.ryder.petmatediarybackend.common.response.Result;
import com.ryder.petmatediarybackend.module.shop.dto.CreatePetCollectionDTO;
import com.ryder.petmatediarybackend.module.shop.dto.CreateProductCollectionDTO;
import com.ryder.petmatediarybackend.module.shop.service.ShopPetCollectionService;
import com.ryder.petmatediarybackend.module.shop.service.ShopProductCollectionService;
import com.ryder.petmatediarybackend.module.shop.vo.PetCollectionVO;
import com.ryder.petmatediarybackend.module.shop.vo.ProductCollectionVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 描述：店铺合集管理 Controller (宠物合集 + 商品合集)
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0
 * 更新日期: 2025-10-31
 */
@Slf4j
@Tag(name = "店铺合集管理", description = "店铺宠物合集和商品合集相关接口（需要登录 + 店铺经营者角色）")
@RestController
@RequestMapping("/shop/collection")
@SecurityRequirement(name = "Authorization")
@RequiredArgsConstructor
public class ShopCollectionController {

    private final ShopPetCollectionService petCollectionService;
    private final ShopProductCollectionService productCollectionService;

    // ==================== 宠物合集管理 ====================

    @Operation(summary = "创建宠物合集", description = "添加店铺宠物，需要 shop:pet:create 权限")
    @SaCheckPermission("shop:pet:create")
    @PostMapping("/pet/create")
    public Result<PetCollectionVO> createPetCollection(@Valid @RequestBody CreatePetCollectionDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[宠物合集] 用户ID={} 执行创建宠物合集, 方法=createPetCollection, 入参={}", userId, dto);
        PetCollectionVO vo = petCollectionService.createPetCollection(userId, dto);
        log.info("[宠物合集] 用户ID={} 创建宠物合集成功, 合集ID={}, 宠物名称={}", userId, vo.getId(), vo.getPetName());
        return Result.ok(vo);
    }

    @Operation(summary = "获取宠物合集列表", description = "获取我的店铺宠物合集，需要 shop:pet:view 权限")
    @SaCheckPermission("shop:pet:view")
    @GetMapping("/pet/list")
    public Result<List<PetCollectionVO>> getMyPetCollections() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[宠物合集] 用户ID={} 执行获取宠物合集列表, 方法=getMyPetCollections", userId);
        List<PetCollectionVO> list = petCollectionService.getMyPetCollections(userId);
        log.info("[宠物合集] 用户ID={} 获取宠物合集列表成功, 数量={}", userId, list.size());
        return Result.ok(list);
    }

    @Operation(summary = "更新宠物合集", description = "更新店铺宠物信息，需要 shop:pet:update 权限")
    @SaCheckPermission("shop:pet:update")
    @PutMapping("/pet/update/{id}")
    public Result<PetCollectionVO> updatePetCollection(
            @PathVariable Long id,
            @Valid @RequestBody CreatePetCollectionDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[宠物合集] 用户ID={} 执行更新宠物合集, 方法=updatePetCollection, 合集ID={}, 入参={}", userId, id, dto);
        PetCollectionVO vo = petCollectionService.updatePetCollection(userId, id, dto);
        log.info("[宠物合集] 用户ID={} 更新宠物合集成功, 合集ID={}", userId, vo.getId());
        return Result.ok(vo);
    }

    @Operation(summary = "删除宠物合集", description = "删除店铺宠物（逻辑删除），需要 shop:pet:delete 权限")
    @SaCheckPermission("shop:pet:delete")
    @DeleteMapping("/pet/delete/{id}")
    public Result<Void> deletePetCollection(@PathVariable Long id) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[宠物合集] 用户ID={} 执行删除宠物合集, 方法=deletePetCollection, 合集ID={}", userId, id);
        petCollectionService.deletePetCollection(userId, id);
        log.info("[宠物合集] 用户ID={} 删除宠物合集成功, 合集ID={}", userId, id);
        return Result.ok();
    }

    @Operation(summary = "上架/下架宠物", description = "更新宠物上架状态")
    @PutMapping("/pet/status/{id}")
    public Result<Void> updatePetStatus(@PathVariable Long id, @RequestParam Integer status) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[宠物合集] 用户ID={} 执行更新宠物状态, 方法=updatePetStatus, 合集ID={}, 状态={}", userId, id, status);
        petCollectionService.updateStatus(userId, id, status);
        log.info("[宠物合集] 用户ID={} 更新宠物状态成功, 合集ID={}", userId, id);
        return Result.ok();
    }

    @Operation(summary = "根据店铺ID获取宠物合集", description = "公开接口，任何人都可以查看")
    @SaCheckPermission("shop:collection:pet:public:view")
    @GetMapping("/pet/shop/{shopId}")
    public Result<List<PetCollectionVO>> getPetCollectionsByShopId(@PathVariable Long shopId) {
        log.info("[宠物合集] 执行根据店铺ID获取宠物合集, 方法=getPetCollectionsByShopId, 店铺ID={}", shopId);
        List<PetCollectionVO> list = petCollectionService.getByShopId(shopId);
        log.info("[宠物合集] 获取宠物合集成功, 店铺ID={}, 数量={}", shopId, list.size());
        return Result.ok(list);
    }

    // ==================== 商品合集管理 ====================

    @Operation(summary = "创建商品合集", description = "添加店铺商品，需要 shop:product:create 权限")
    @SaCheckPermission("shop:product:create")
    @PostMapping("/product/create")
    public Result<ProductCollectionVO> createProductCollection(@Valid @RequestBody CreateProductCollectionDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[商品合集] 用户ID={} 执行创建商品合集, 方法=createProductCollection, 入参={}", userId, dto);
        ProductCollectionVO vo = productCollectionService.createProductCollection(userId, dto);
        log.info("[商品合集] 用户ID={} 创建商品合集成功, 合集ID={}, 商品名称={}", userId, vo.getId(), vo.getProductName());
        return Result.ok(vo);
    }

    @Operation(summary = "获取商品合集列表", description = "获取我的店铺商品合集，需要 shop:product:view 权限")
    @SaCheckPermission("shop:product:view")
    @GetMapping("/product/list")
    public Result<List<ProductCollectionVO>> getMyProductCollections() {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[商品合集] 用户ID={} 执行获取商品合集列表, 方法=getMyProductCollections", userId);
        List<ProductCollectionVO> list = productCollectionService.getMyProductCollections(userId);
        log.info("[商品合集] 用户ID={} 获取商品合集列表成功, 数量={}", userId, list.size());
        return Result.ok(list);
    }

    @Operation(summary = "更新商品合集", description = "更新店铺商品信息，需要 shop:product:update 权限")
    @SaCheckPermission("shop:product:update")
    @PutMapping("/product/update/{id}")
    public Result<ProductCollectionVO> updateProductCollection(
            @PathVariable Long id,
            @Valid @RequestBody CreateProductCollectionDTO dto) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[商品合集] 用户ID={} 执行更新商品合集, 方法=updateProductCollection, 合集ID={}, 入参={}", userId, id, dto);
        ProductCollectionVO vo = productCollectionService.updateProductCollection(userId, id, dto);
        log.info("[商品合集] 用户ID={} 更新商品合集成功, 合集ID={}", userId, vo.getId());
        return Result.ok(vo);
    }

    @Operation(summary = "删除商品合集", description = "删除店铺商品（逻辑删除），需要 shop:product:delete 权限")
    @SaCheckPermission("shop:product:delete")
    @DeleteMapping("/product/delete/{id}")
    public Result<Void> deleteProductCollection(@PathVariable Long id) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[商品合集] 用户ID={} 执行删除商品合集, 方法=deleteProductCollection, 合集ID={}", userId, id);
        productCollectionService.deleteProductCollection(userId, id);
        log.info("[商品合集] 用户ID={} 删除商品合集成功, 合集ID={}", userId, id);
        return Result.ok();
    }

    @Operation(summary = "上架/下架商品", description = "更新商品上架状态")
    @PutMapping("/product/status/{id}")
    public Result<Void> updateProductStatus(@PathVariable Long id, @RequestParam Integer status) {
        Long userId = StpUtil.getLoginIdAsLong();
        log.info("[商品合集] 用户ID={} 执行更新商品状态, 方法=updateProductStatus, 合集ID={}, 状态={}", userId, id, status);
        productCollectionService.updateStatus(userId, id, status);
        log.info("[商品合集] 用户ID={} 更新商品状态成功, 合集ID={}", userId, id);
        return Result.ok();
    }

    @Operation(summary = "根据店铺ID获取商品合集", description = "公开接口，任何人都可以查看")
    @SaCheckPermission("shop:collection:product:public:view")
    @GetMapping("/product/shop/{shopId}")
    public Result<List<ProductCollectionVO>> getProductCollectionsByShopId(@PathVariable Long shopId) {
        log.info("[商品合集] 执行根据店铺ID获取商品合集, 方法=getProductCollectionsByShopId, 店铺ID={}", shopId);
        List<ProductCollectionVO> list = productCollectionService.getByShopId(shopId);
        log.info("[商品合集] 获取商品合集成功, 店铺ID={}, 数量={}", shopId, list.size());
        return Result.ok(list);
    }
}

